package Slots;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Display extends JPanel implements ActionListener {

    private Console console;
    private String message = "Bienvenue!";
    public Graphics2D g2d;
    public int height = 153;
    public int x = 90;
    public int vitesse = 19;
    public Timer timer;
    public int i = 1;
    public int j = 1;
    public Image IMAGE_SLOTS1 = Toolkit.getDefaultToolkit().createImage("Images/Slots3.JPG");
    public Image IMAGE_SLOTS2 = Toolkit.getDefaultToolkit().createImage("Images/Slots4.JPG");

    /**
     * Constructeur de la classe Display
     * @param console la console dans laquelle se situe le panneau de diffusion
     */
    public Display(Console console) {
        this.console = console;
        this.setOpaque(false);
        timer = new Timer(vitesse, this);
        timer.start();
//        messageInvit.
    }

    /**
     * @param message le message diffusé par le panneau
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return message le message diffusé par le panneau
     */
    public String getMessage() {
        return message;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == timer) {

            repaint();
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        FontMetrics fm = g.getFontMetrics();
        g2d = (Graphics2D) g;
        g2d.setFont(new Font("Arial", 1, 14));
        g2d.setColor(Color.RED);

        int stringWidth = fm.stringWidth(message);
        int stringLength = message.length();
        int lengthIncr = 0;

//        if (x < 250) {
            g2d.drawString(message, x - stringWidth, height);
            x++;
//        }
//        if (x == 250) {
//            for (int j = 0; j < stringLength; j++) {
//                lengthIncr = 0;
//                for (int i = 0; i < stringLength; i++) {
//                    lengthIncr += fm.stringWidth(message.substring(i + j, i + j + 1));
//                    if (i % 2 == 0) {
//                        g2d.drawString(message.substring(i + j, i + j + 1), x - stringWidth + lengthIncr, height);
//                    }
//                }
//            }
//        }
        if (x == 456 + stringWidth) {
            x = 90;
        }
        g.drawImage(IMAGE_SLOTS1, 0, 131, null);
        g.drawImage(IMAGE_SLOTS2, 465, 131, null);

    }
}
